<?php

namespace App\Http\Controllers\Admin\Finance;

use App\Http\Controllers\Controller;
use App\Http\Requests\finance\TransferPayRequest;
use App\Models\AgentModel;
use App\Models\finance\TransferPayModel;
use Illuminate\Http\Request;

class TransferPay extends Controller
{
    use TransferPayRequest;

    /**
     * Notes:列表
     * User: xiangzi
     * Date: 2024/9/5
     * Time: 14:35
     * @return \Illuminate\Http\JsonResponse
     */
    public function list(Request $request) {
        $page = $request->input('page', 1);
        $limit = $request->input('limit', 20);
        $agent_name = $request->input('agent_name', '');
        $create_start = $request->input('create_start', '');
        $create_end = $request->input('create_end', '');
        $creator = $request->input('creator', '');

        $query = TransferPayModel::query()->withWhereHas('agent', function ($query) use ($agent_name) {
            if($agent_name) {
                $query->where('name', 'like', "%$agent_name%");
            }
        })->withWhereHas('create_admin', function ($query) use ($creator) {
            if($creator) {
                $query->withWhereHas('staff', function ($query) use ($creator){
                    $query->where('name', 'like', "%$creator%");
                });
            }
        });

        if($create_start && $create_end){
            $query = $query->whereBetween('created_at', [$create_start, $create_end]);
        }

        $total = $query->count('id');
        $total_amount = $query->sum('amount');
        $items = $query
            ->offset(($page-1) * $limit)
            ->limit($limit)
            ->orderBy('id', 'desc')
            ->get();

        $list = $items->map(function($item) {
            $res = [
                'id' => $item->id,
                'agent_name' => $item->agent ? $item->agent->name : '',
                'account_name' => $item->agent ? $item->agent->account_name : '',
                'bank' => $item->agent ? $item->agent->bank : '',
                'bank_card' => $item->agent ? $item->agent->bank_card : '',
                'amount' => $item->amount,
                'files' => json_decode($item->files, true),
                'remarks' => $item->remarks,
                'created_at' => $item->created_at->toDateTimeString(),
                'create_user' => $item->create_admin ? $item->create_admin->staff->name : '',
            ];
            return $res;
        });

        return $this->success(['list' => $list, 'total' => $total, 'total_amount' => $total_amount]);
    }

    /**
     * Notes:添加
     * User: xiangzi
     * Date: 2024/9/5
     * Time: 14:35
     * @return \Illuminate\Http\JsonResponse
     */
    public function add(Request $request){
        $auth = $request->get('auth');
        $params = $request->all();

        $check_res = self::add_check($params);
        if($check_res->faild){
            return $this->error($check_res->err);
        }

        $model = new TransferPayModel();
        $model->agent_id = $params['agent_id'];
        $model->take_order_sn = json_encode($params['take_order_sn']);
        $model->amount = $params['amount'];
        $model->files = json_encode($params['files']);
        $model->remarks = $params['remarks'] ?? '';
        $model->created_admin_id = $auth->id;
        $model->save();

        return $this->success();
    }

    /**
     * Notes:获取代理账户信息
     * User: xiangzi
     * Date: 2024/10/14
     * Time: 下午2:27
     * @return \Illuminate\Http\JsonResponse
     */
    public function get_account(Request $request)
    {
        $agent_id = $request->input('agent_id', '');
        $model = AgentModel::find($agent_id);
        if(!$model){
            return $this->error('未找到该服务提供商');
        }
        return $this->success(['account_name' => $model->account_name, 'bank' => $model->bank, 'bank_card' => $model->bank_card]);
    }

}
